package com.guangyunl.b_array;

import java.util.Arrays;

// 数组扩容
//需求:
//    定义一个数组: int[] arr1 = {1,2,3,4,5}
//        将数组由原来的长度扩容到10
//方式二:
//  使用 Arrays.copyOf()
public class Demo08_02ArrayExpand {
    public static void main(String[] args) {
        // 定义数组arr1[]和数组arr2[]
        int[] arr1 = {1,2,3,4,5};
        // 计算新数组的长度
        int[] result = Arrays.copyOf(arr1, 10);
        // 将 result 数组赋值给 arr1
        arr1 = result;

        // 打印结果数组
        System.out.println("arr1 = " + Arrays.toString(arr1));
        for (int i = 0; i < arr1.length; i++) {
            System.out.println("arr1[" + i + "] = " + arr1[i]);
        }
        System.out.println("arr1.length = " + arr1.length);
    }
}

/*
arr1 = [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
arr1[0] = 1
arr1[1] = 2
arr1[2] = 3
arr1[3] = 4
arr1[4] = 5
arr1[5] = 0
arr1[6] = 0
arr1[7] = 0
arr1[8] = 0
arr1[9] = 0
arr1.length = 10
 */